package com.example.onlinemonitoring.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.onlinemonitoring.model.common.*;
import com.example.onlinemonitoring.query.QueryObject;
import com.example.onlinemonitoring.model.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.example.onlinemonitoring.serviceImpl.SctmsensorServiceImpl;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * Controller
 * 该部分代码为自动生成，请勿随意修改
 * 如有改动请删除该注释
 * 
 * @author little pig page
 * @date 2022-04-11
 */
@Api(value = "开关柜测温设备", tags = { "开关柜测温设备接口" })
@RestController
@RequestMapping("/switchcabinet")
public class SctmsensorController {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    @Autowired
    SctmsensorServiceImpl sctmserviceimpl;

    @ApiOperation(value = "修改设备参数", notes = "修改设备参数")
    @ResponseBody
    @RequestMapping(value = "/updateAll", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateAll(@RequestBody String jsonParam,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        Sctmsensor record = object.toJavaObject(Sctmsensor.class);
        MyResult<Integer> m = new MyResult<Integer>();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        record.setDeviceaddr(deviceaddr);
        record.setPointaddr(pointaddr);
        if (sctmserviceimpl.Update(record, false) >= 1) {
            m.setMsg("修改成功");
            m.setCode(0);
        } else {
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "修改设备报警", notes = "修改设备报警")
    @ResponseBody
    @RequestMapping(value = "/updateAlarm", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateAlarm(@RequestBody String jsonParam,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Sctmsensor record = new Sctmsensor();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        record.setDeviceaddr(deviceaddr);
        record.setPointaddr(pointaddr);
        record.setAcablealarmrelievevalue(object.getFloat("acablealarmrelievevalue"));
        record.setAcablealarmvalue(object.getFloat("acablealarmvalue"));
        record.setAcablewarnrelievevalue(object.getFloat("acablewarnrelievevalue"));
        record.setAcablewarnvalue(object.getFloat("acablewarnvalue"));
        record.setAlowcontactalarmrelievevalue(object.getFloat("alowcontactalarmrelievevalue"));
        record.setAlowcontactalarmvalue(object.getFloat("alowcontactalarmvalue"));
        record.setAlowcontactwarnrelievevalue(object.getFloat("alowcontactwarnrelievevalue"));
        record.setAlowcontactwarnvalue(object.getFloat("alowcontactwarnvalue"));
        record.setAsparealarmrelievevalue(object.getFloat("asparealarmrelievevalue"));
        record.setAsparealarmvalue(object.getFloat("asparealarmvalue"));
        record.setAsparewarnrelievevalue(object.getFloat("asparewarnrelievevalue"));
        record.setAsparewarnvalue(object.getFloat("asparewarnvalue"));
        record.setAupcontactalarmrelievevalue(object.getFloat("aupcontactalarmrelievevalue"));
        record.setAupcontactalarmvalue(object.getFloat("aupcontactalarmvalue"));
        record.setAupcontactwarnrelievevalue(object.getFloat("aupcontactwarnrelievevalue"));
        record.setAupcontactwarnvalue(object.getFloat("aupcontactwarnvalue"));
        record.setBatteryvoltagealarmrelievevalue(object.getInteger("batteryvoltagealarmrelievevalue"));
        record.setBatteryvoltagealarmvalue(object.getInteger("batteryvoltagealarmvalue"));
        record.setBatteryvoltagewarnrelievevalue(object.getInteger("batteryvoltagewarnrelievevalue"));
        record.setBatteryvoltagewarnvalue(object.getInteger("batteryvoltagewarnvalue"));
        record.setBcablealarmrelievevalue(object.getFloat("bcablealarmrelievevalue"));
        record.setBcablealarmvalue(object.getFloat("bcablealarmvalue"));
        record.setBcablewarnrelievevalue(object.getFloat("bcablewarnrelievevalue"));
        record.setBcablewarnvalue(object.getFloat("bcablewarnvalue"));
        record.setBlowcontactalarmrelievevalue(object.getFloat("blowcontactalarmrelievevalue"));
        record.setBlowcontactalarmvalue(object.getFloat("blowcontactalarmvalue"));
        record.setBlowcontactwarnrelievevalue(object.getFloat("blowcontactwarnrelievevalue"));
        record.setBlowcontactwarnvalue(object.getFloat("blowcontactwarnvalue"));
        record.setBsparealarmrelievevalue(object.getFloat("bsparealarmrelievevalue"));
        record.setBsparealarmvalue(object.getFloat("bsparealarmvalue"));
        record.setBsparewarnrelievevalue(object.getFloat("bsparewarnrelievevalue"));
        record.setBsparewarnvalue(object.getFloat("bsparewarnvalue"));
        record.setBupcontactalarmrelievevalue(object.getFloat("bupcontactalarmrelievevalue"));
        record.setBupcontactalarmvalue(object.getFloat("bupcontactalarmvalue"));
        record.setBupcontactwarnrelievevalue(object.getFloat("bupcontactwarnrelievevalue"));
        record.setBupcontactwarnvalue(object.getFloat("bupcontactwarnvalue"));
        record.setCcablealarmrelievevalue(object.getFloat("ccablealarmrelievevalue"));
        record.setCcablealarmvalue(object.getFloat("ccablealarmvalue"));
        record.setCcablewarnrelievevalue(object.getFloat("ccablewarnrelievevalue"));
        record.setCcablewarnvalue(object.getFloat("ccablewarnvalue"));
        record.setClowcontactalarmrelievevalue(object.getFloat("clowcontactalarmrelievevalue"));
        record.setClowcontactalarmvalue(object.getFloat("clowcontactalarmvalue"));
        record.setClowcontactwarnrelievevalue(object.getFloat("clowcontactwarnrelievevalue"));
        record.setClowcontactwarnvalue(object.getFloat("clowcontactwarnvalue"));
        record.setCsparealarmrelievevalue(object.getFloat("csparealarmrelievevalue"));
        record.setCsparealarmvalue(object.getFloat("csparealarmvalue"));
        record.setCsparewarnrelievevalue(object.getFloat("csparewarnrelievevalue"));
        record.setCsparewarnvalue(object.getFloat("csparewarnvalue"));
        record.setCupcontactalarmrelievevalue(object.getFloat("cupcontactalarmrelievevalue"));
        record.setCupcontactalarmvalue(object.getFloat("cupcontactalarmvalue"));
        record.setCupcontactwarnrelievevalue(object.getFloat("cupcontactwarnrelievevalue"));
        record.setCupcontactwarnvalue(object.getFloat("cupcontactwarnvalue"));
        if (sctmserviceimpl.Update(record, false) >= 1) {
            m.setMsg("修改成功");
            m.setCode(0);
        } else {
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "查询实时表数据", notes = "查询实时表数据")
    @ResponseBody
    @GetMapping("/selectSensorData")
    public MyResult<Sctmsensor> selectSensorData(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
            @RequestParam(value = "inuse", required = false) Integer inuse,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        Sctmsensor record = new Sctmsensor();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        record.setPointaddr(pointaddr);
        record.setInuse(inuse);
        QueryObject qo = new QueryObject(page, limit);
        return sctmserviceimpl.getList(record, qo);
    }

    @ApiOperation(value = "修改监测点名称", notes = "修改监测点名称")
    @ResponseBody
    @RequestMapping(value = "/updateMonitorName", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateMonitorName(@RequestBody String jsonParam,
            @RequestParam(value = "id", required = false) Long id) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Sctmsensor record = new Sctmsensor();
        record.setId(id);
        record.setPointname(object.getString("pointname"));
        if (sctmserviceimpl.Update(record, true) >= 1) {
            m.setMsg("修改成功");
            m.setCode(0);
        } else {
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "查询历史表数据", notes = "查询历史表数据")
    @ResponseBody
    @GetMapping("/selectHistoryData")
    public MyResult<Sctmhistory> selectHistoryData(
            @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime,
            @RequestParam(value = "pagecount", required = false) Integer pagecount) {
        Sctmhistory record = new Sctmhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        pagebase.setPage(page);
        pagebase.setLimit(limit);
        pagebase.setPagecount(pagecount);
        record.setPointaddr(pointaddr);
        return sctmserviceimpl.getHisList(record, pagebase);
    }

    @ApiOperation(value = "查询历史表曲线", notes = "查询历史表曲线")
    @ResponseBody
    @GetMapping("/selectHistoryCurve")
    public MyResult<chartres> selectHistoryCurve(
            @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit,
            @RequestParam(value = "stationid", required = true) Integer stationid,
            @RequestParam(value = "deviceaddr", required = true) Integer deviceaddr,
            @RequestParam(value = "pointaddr", required = true) Integer pointaddr,
            @RequestParam(value = "devicegroup", required = true) Integer devicegroup,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime,
            @RequestParam(value = "pagecount", required = false) Integer pagecount,
            @RequestParam(value = "lstr", required = false) String lstr) {

        Sctmhistory record = new Sctmhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        pagebase.setPage(page);
        pagebase.setLimit(limit);
        pagebase.setIsasc(true);
        pagebase.setPagecount(pagecount);
        record.setPointaddr(pointaddr);
        return sctmserviceimpl.getHisCurveList(record, lstr, pagebase);
    }

    @ApiOperation(value = "关联监测点", notes = "关联监测点")
    @ResponseBody
    @GetMapping(value = "/updateInuse")
    public MyResult<Integer> updateInuse(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "inuse", required = true) Integer inuse) {
        MyResult<Integer> m = new MyResult<Integer>();
        Sctmsensor record = new Sctmsensor();
        record.setId(id);
        record.setInuse(inuse);
        if (sctmserviceimpl.Update(record, true) >= 1) {
            m.setMsg("修改成功");
            m.setCode(0);
        } else {
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "关联监测点（范围添加）", notes = "关联监测点（范围添加）")
    @ResponseBody
    @RequestMapping(value = "/updateInuses", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateInuses(@RequestBody String jsonParam,
            @RequestParam(value = "stationid", required = true) Integer stationid,
            @RequestParam(value = "devicegroup", required = true) Integer devicegroup) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        repointaddr record = object.toJavaObject(repointaddr.class);
        return sctmserviceimpl.UpdateInuse(record, stationid, devicegroup);
    }

    @ApiOperation(value = "查询监测点关联", notes = "查询监测点关联")
    @ResponseBody
    @GetMapping("/selectInuse")
    public MyResult<repointaddr> selectInuse(@RequestParam(value = "stationid", required = true) Integer stationid,
            @RequestParam(value = "devicegroup", required = true) Integer devicegroup) {
        Sctmsensor record = new Sctmsensor();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        return sctmserviceimpl.SelectInuse(record);
    }

    @ApiOperation(value = "查询地址名称", notes = "查询地址名称")
    @ResponseBody
    @GetMapping("/selectAddrInfo")
    public MyResult<addrinfo> selectAddrInfo(@RequestParam(value = "stationid", required = true) Integer stationid,
            @RequestParam(value = "devicegroup", required = true) Integer devicegroup) {
        Sctmsensor record = new Sctmsensor();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        return sctmserviceimpl.SelectAddrInfo(record);
    }

    /**
     * 导出Excel
     */
    @GetMapping("exportToExcel")
    public void exportToExcel(HttpServletResponse response,
            @RequestParam(value = "stationid", required = true) Integer stationid,
            @RequestParam(value = "devicegroup", required = true) Integer devicegroup,
            @RequestParam(value = "deviceaddr", required = true) Integer deviceaddr,
            @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime) throws IOException {
        Sctmhistory record = new Sctmhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        record.setPointaddr(pointaddr);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        Workbook wb = sctmserviceimpl.writeToExcelByList(record, pagebase);
        OutputStream output = response.getOutputStream();
        String fileName = "data.xlsx";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition",
                "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
        wb.write(output);
        output.close();
    }
}
